<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
date_default_timezone_set('Asia/Ho_Chi_Minh');
function convert_hour_to_float($h){
    if($h==''){
        return '';
    }else {
        $arr = @split(":", $h);
        return $arr[0] + $arr[1]/60;
    }
}
function convert_float_to_hour($f){
     $h = (int)($f);
     $m = ($f*10%10)*6;
     return $h.'h'.$m;
}
function fillDropdown($css_id,$css_class,$name,$arr,$column_id,$colum_name,$id_selected){
   $str = "<select";
   if($css_id!=null) $str.= " id ='$css_id'";
   if($css_class!=null) $str.= " class ='$css_class'";
   if($name!=null) $str.= " name ='$name' ";
   $str .= '>';
   if($id_selected!=null){
       foreach ($arr as  $a) {
           if($a[$column_id]==$id_selected){
               $str .= "<option selected='selected' value='".$a[$column_id]."'>".$a[$colum_name].'</option>';
           }else {
               $str .= "<option  value='".$a[$column_id]."'>".$a[$colum_name].'</option>';
           }
       }
   }else{
       foreach ($arr as  $a) {
            $str .= "<option  value='".$a[$column_id]."'>".$a[$colum_name].'</option>';
       }
   }
   $str .='</select>';
   return $str;
}
function datephp($strdate){
    $date = date_create($strdate);
    return date_format($date, 'd-m-Y');
}
function dateNow(){
     $d = date('Y-m-d');
     $d = new DateTime();
     //$d->modify("+6 hours");
     return $d;
 }
function datetimephp($strdate){
    $date = date_create($strdate);
    return date_format($date, 'd-m-Y h:m:s');
}
function datemysql($strdate){
    $date = date_create($strdate);
    return @date_format($date,'Y-m-d');
}
function arrTostr($arr){
    $json = json_encode($arr);
    $phpStringArray = str_replace(array("{","}",":"), array("array(","}","=>"), $json);
    return $phpStringArray;
}
function arrTotable($arr,$html=false){
    $str = "";
    if($html) {
        $str.= "<html xmlns='http://www.w3.org/1999/xhtml'><head><meta http-equiv='Content-Type' content='text/html;charset=utf-8' />";
    }
    $str .= "<style> #tabledemo123321 th{font-weight: bold;}#tabledemo123321 tr,#tabledemo123321 tr th,#tabledemo123321 tr td {border: 1px solid #000000}</style>";
    if($html) {
        $str.= "</head><body>";
    }
    $str .= "<table id='tabledemo123321'><tr>";
    foreach ($arr[0] as $k => $a) {
        $str.= "<th>$k</th>";
    }
    $str .= "</tr>";
    foreach ($arr as $a) {
        $str.= "<tr>";
        foreach ($a as $v) {
            $str .= "<td>$v</td>";
        }
        $str.="</tr>";
    }
    $str .= "</table>";
    if($html) {
        $str.= "</body>";
    }
    return $str;
}
function arrTooptions($arr,$name,$value,$chose=false){
    $str = '';
    if($chose) $str.= "<option value='0'>---- Chọn ----</option>";
    foreach ($arr as $a) {
        $str.= "<option value='".$a[$value]."'>".$a[$name].'</option>';
    }
    return $str;
}
function arrTooptions2($arr,$name,$value,$attr_label,$atrr_val){
    $str = '';
    foreach ($arr as $a) {
        $v = $a[$value];
        $n = $a[$name];
        $a = $a[$atrr_val];
        $str.= "<option value='$v' $attr_label='$a'>$n</option>";
    }
    return $str;
}


 function works_multi_level($arrData, $parentid = 0, $level = 0, &$result) {
    if (count($arrData) > 0) {
        foreach ($arrData as $key => $val) { // Sai khi ko có dòng nào có parentid = 0
            if ($parentid == $val['parentid']) {
                $val['level'] = $level;
                $result[] = $val;
                $_parentid = $val['id'];
                unset($arrData[$key]);
                works_multi_level($arrData, $_parentid, $level + 1, $result);
            }
        }
    }
 }
 function works_multi_level_form($arrData,$selected_id=null){
        works_multi_level($arrData, 0, 0, $result);
        $CI =& get_instance();
        $CI->load->model('work_model');
        print_r($arrData);
        foreach ($result as $key => $val) {
            $numRepeat = $val['level'];
            $char = '';
            if ($numRepeat > 0) {
                $char .= str_repeat('------', $numRepeat);
            }
            if($CI->work_model->is_assign($val['id'])){
                if($selected_id==null||$val['id']!=$selected_id){
                echo "<option value='".$val['id']."'>".$char . ' ' . $val['name'].'&nbsp;&nbsp;(Đã phân công)</option>';
                }else {
                    echo "<option selected='true' value='".$val['id']."'>".$char . ' ' . $val['name'].'&nbsp;&nbsp;(Đã phân công)</option>';
                }
            }else {
                if($selected_id==null||$val['id']!=$selected_id){
                echo "<option value='".$val['id']."'>".$char . ' ' . $val['name'].'</option>';
                }else {
                    echo "<option selected='true' value='".$val['id']."'>".$char . ' ' . $val['name'].'</option>';
                }
            }   
        }
		
 }

// Hàm ở trên có một lỗ hổng là khi ko có bảng ghi nào có parentid =0, nên dùng hàm này 
  function works_multi_level_form2($arrData,$selected_id=null){
  		if(count($arrData)==0) return;
		$paid = $arrData[0]['parentid'];
		if(count($arrData)>1){
				foreach ($arrData as $k => $v) {
					$paid = $paid > $arrData[$k]['parentid'] ? $arrData[$k]['parentid'] : $paid;
				}  	  
		}
  		
        works_multi_level($arrData,$paid, 0, $result);
        $CI =& get_instance();
        $CI->load->model('work_model');
        print_r($arrData);
        foreach ($result as $key => $val) {
            $numRepeat = $val['level'];
            $char = '';
            if ($numRepeat > 0) {
                $char .= str_repeat('------', $numRepeat);
            }
            if($CI->work_model->is_assign($val['id'])){
                if($selected_id==null||$val['id']!=$selected_id){
                echo "<option value='".$val['id']."'>".$char . ' ' . $val['name'].'&nbsp;&nbsp;(Đã phân công)</option>';
                }else {
                    echo "<option selected='true' value='".$val['id']."'>".$char . ' ' . $val['name'].'&nbsp;&nbsp;(Đã phân công)</option>';
                }
            }else {
                if($selected_id==null||$val['id']!=$selected_id){
                echo "<option value='".$val['id']."'>".$char . ' ' . $val['name'].'</option>';
                }else {
                    echo "<option selected='true' value='".$val['id']."'>".$char . ' ' . $val['name'].'</option>';
                }
            }   
        }
		
 }
  
  function works_multi_level_table($arrData){
        works_multi_level($arrData, 0, 0, $result);
        $CI =& get_instance();
        $CI->load->model('work_model');
        foreach ($result as $key => $val) {
            $numRepeat = $val['level'];
            $char = '';
            if ($numRepeat > 0) {
                $char .= str_repeat('------', $numRepeat);
            }
            $result[$key]['name'] = $char . ' ' . $val['name'];
        }
       // print_r($result);
        return $result;
 }
 
 
 function error(){
     redirect(base_url().'error');
 }
 function preUrl(){
     redirect($_SERVER['HTTP_REFERER']);
 }
 function remove_vi ($str){
    $unicode = array(
        'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ',
        'd'=>'đ',
        'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
        'i'=>'í|ì|ỉ|ĩ|ị',
        'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
        'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
        'y'=>'ý|ỳ|ỷ|ỹ|ỵ',
        'A'=>'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
        'D'=>'Đ',
        'E'=>'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
        'I'=>'Í|Ì|Ỉ|Ĩ|Ị',
        'O'=>'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
        'U'=>'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
        'Y'=>'Ý|Ỳ|Ỷ|Ỹ|Ỵ',
    );
    foreach($unicode as $nonUnicode=>$uni){
        $str = preg_replace("/($uni)/i", $nonUnicode, $str);
    }
    return $str;
}
 function utf8(){
 	echo "<meta http-equiv='Content-Type' content='text/html;charset=utf-8' />";
 }

?>